package com.yd.mvc.pc.jewellery;

import com.yd.app.service.pc.jewellery.jewelleryPageService.WebEvaluateService;
import com.yd.app.util.BusinessException;
import com.yd.domain.business.Evaluate;
import com.yd.domain.sys.User;
import com.yd.mvc.baseController.util.AuthUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * Created by sunxianhui on 2018/5/16.
 */
@RestController
@RequestMapping(value = "/api/webEvaluate")
public class WebEvaluateController {
    @Autowired
    private WebEvaluateService webEvaluateService;
    /**
     * 根据orderId,goodsId查询评价
     * @param orderId
     * @param goodsId
     * @return
     */
    @RequestMapping(value = "/getEvaluate/{orderId}/{goodsId}", method = RequestMethod.GET)
    public Evaluate getEvaluate(@PathVariable("orderId") String orderId, @PathVariable("goodsId") String goodsId) throws BusinessException {
        Evaluate evaluate=webEvaluateService.getEvaluate(orderId,goodsId);
        return evaluate;
    }

    /**
     * 添加
     * @param list
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public void addEvaluate(@RequestBody List<Evaluate> list, HttpSession session) throws BusinessException {
        AuthUtil.AuthedUser u = AuthUtil.getCurrentUser();
        for(Evaluate evaluate:list){
            evaluate.setPublishTime(new Date());
            evaluate.setPublishUser(u.getUserId());
            webEvaluateService.deleteEvaluate(evaluate.getOrderId(),evaluate.getGoodsId());
            int count= webEvaluateService.addEvaluate(evaluate);
        }
    }
}
